package com.blue.eagle.controller;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.blue.eagle.dao.CustomerDao;
import com.blue.eagle.domain.Customer;
import com.blue.eagle.formbean.CustomerForm;
import com.blue.eagle.formbean.LoginForm;
import com.blue.eagle.formbean.UploadForm;
import com.blue.eagle.service.ExcelParser;

@Controller

public class CustomerController {
	
	/**
	 * @uml.property  name="customerDao"
	 * @uml.associationEnd  readOnly="true"
	 */
	@Autowired
	CustomerDao customerDao ;
	
	/**
	 * @uml.property  name="message"
	 * @uml.associationEnd  readOnly="true"
	 */
	@Autowired
	MessageSource message ;
	
	@RequestMapping(value = "addCustomer",method = RequestMethod.POST)
	public ModelAndView addCustomer(@ModelAttribute("customerForm") CustomerForm custFormInfo,HttpServletRequest request) {
		
		ModelAndView view = null ;
		System.out.println("Add Customer : "+custFormInfo);
		try {
			//if (custFormInfo != null) {
				Customer customer = new Customer();
				System.out.println("First Name : "+custFormInfo.getFirstName());
				customer.setFirstName(custFormInfo.getFirstName());
				customer.setLastName(custFormInfo.getLastName());
				customer.setMobileNumber(custFormInfo.getMobileNumber());
				customer.setUserName(custFormInfo.getUsername());
				customer.setPassword(custFormInfo.getPassword());
				
				customerDao.save(customer);
				
				view = new ModelAndView("updateCustomer");
				view.addObject("success",message.getMessage("customer.save.information", null, Locale.US));
				
//			}else {
//				view = new ModelAndView("updateCustomer");
//			}
			
		}catch(Exception exe) {
			exe.printStackTrace();
			view = new ModelAndView("updateCustomer");
		}
		
		return view ;
		
	}
	
	@RequestMapping(value = "updateCustomer", method = RequestMethod.GET)
	public ModelAndView initializeCustomer (@ModelAttribute("customerForm") CustomerForm custFormInfo,HttpServletRequest request) {
		
		return new ModelAndView("updateCustomer");
	}
	
	/**
	 * @uml.property  name="parser"
	 * @uml.associationEnd  readOnly="true"
	 */
	@Autowired
	ExcelParser parser ;
	
	@RequestMapping(value = "uploadFile", method = RequestMethod.POST)
	public ModelAndView handleFileUpload(@ModelAttribute("uploadForm") UploadForm uploadForm,
			@RequestParam ("file")MultipartFile file) {
		
		System.out.println("File Path from formbean : "+uploadForm.getFile().getOriginalFilename());
		if(!file.isEmpty()) {
			System.out.println("File Path from request param : "+file.getOriginalFilename());
			File oFile = null ;
			try {
				/**
				 * - Validate Input File
				 * - upload File
				 * - Process, parse the file 
				 * - save the data as collection.
				 */
				oFile = new File("/Users/Meet/CustomerInfo.xsl");
				file.transferTo(oFile);
				System.out.println(oFile.getName());
				
				//if (validateFile(oFile)) {
					parser.parseFile(oFile);
					
				//}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return new ModelAndView("welcomePage");
	}
	
	@RequestMapping(value= "resource/loginSuccess", method = RequestMethod.GET)
	public ModelAndView redirectToWelcome(@ModelAttribute("loginForm") LoginForm loginForm,HttpServletRequest request) {
		System.out.println("Single Sign ON ....");
		ModelAndView model = new ModelAndView ("welcomePage");
		model.addObject("praise","Welcome !" );
			
		model.addObject("customerList", customerDao.getCustomerList());
		model.addObject("uploadForm",new UploadForm());
		
		return model;
		
	}

}
